﻿using DotLiquid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dot.Js
{
    public class Person : ILiquidizable
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public object ToLiquid()
        {
            return new
            {
                Name = Name,
                Age = Age
            };
        }
    }



    public class TemplateEngine
    {
        public TemplateEngine()
        {

        }
        public void Demo2()
        {
            List<Person> people = new List<Person>
                {
                    new Person { Name = "John", Age = 30 },
                    new Person { Name = "Jane", Age = 25 },
                    // Add more people as needed
                };


            var templateString = "<ul>\r\n  {% for person in people %}\r\n    <li>{{ person.Name }} - {{ person.Age }}</li>\r\n  {% endfor %}\r\n</ul>";
            var template = Template.Parse(templateString);
            var result = template.Render(Hash.FromAnonymousObject(new { people = people }));
            Console.WriteLine(result);
        }


        public void Demo1()
        {
            Liquid.UseRubyDateFormat = true;
            var temp = "{% if showHeader %}\r\n  <h1>{{ title }}</h1>\r\n{% endif %}\r\n\r\n<ul>\r\n  {% for item in items %}\r\n    <li>{{ item }}</li>\r\n  {% endfor %}\r\n</ul>\r\n";
            // 准备数据集合
            // 准备数据集合
            var data = new
            {
                showHeader = true, // 设置条件变量
                title = "My List",
                items = new List<string> { "Item 1", "Item 2", "Item 3" }
            };


            var result = RenderTemplate(temp, data);
            Console.WriteLine(result);
        }

        public string RenderTemplate(string templateString, object variables)
        {
            Template template = Template.Parse(templateString); // Compile the template
            return template.Render(Hash.FromAnonymousObject(variables)); // Merge with provided variable objects and generate output
        }
    }
}
